package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @description: 检查项的web层
 * @author: wangjingliang
 * @create: 2019-11-06 20:25
 **/
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;

    /**
     * @param: 添加
     * @return: null
     * @date: 2019/11/6
     */
    @PostMapping("/add")
    //@RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        int param;
        try {
            param = checkItemService.addAll(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL, e.getMessage());
        }

        if (param == 0) {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL, "服务器正忙...");
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 根据条件进行分页查询
     *
     * @param: QueryPageBean
     * @return: null
     * @date: 2019/11/6
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = null;
        try {
            pageResult = checkItemService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, "服务器正忙");
        }
    }


    /**
     * 根据id进行查询
     *
     * @param:Integer id
     * @return: List<CheckItem>
     * @date: 2019/11/7
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {

        CheckItem byId = null;
        try {
            byId = checkItemService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, e.getMessage());
        }
        if (byId == null) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, "系统正忙......");
        }

        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, byId);
    }

    /**
     * 根据提供数据进行修改
     *
     * @param: CheckItem
     * @return: int
     * @date: 2019/11/7
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        int edit = 0;
        try {
            edit = checkItemService.edit(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL, e.getMessage());
        }
        if (edit == 0) {
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL, "服务器正忙....");
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);

    }

    /**
     * 根据id进行删除
     *
     * @param:id
     * @return: int
     * @date: 2019/11/7
     */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//权限校验
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        int deleteById;
        try {
            deleteById = checkItemService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL, e.getMessage());
        }
        if (deleteById == 0) {
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL, "服务器正忙...");
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 查询所有
     * url:/checkitem/findAll.do
     *
     * @param:null
     * @return:List<CheckItem>
     * @date: 2019/11/8
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<CheckItem> checkItemList = null;
        try {
            checkItemList = checkItemService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, "系统正忙....");
        }
        if (checkItemList == null) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, "系统正忙....");

        }
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
    }
}
